package de.htwsaar.smog.datagenerator;

import java.net.UnknownHostException;
import java.util.Arrays;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;

public class Main {

	public static void main(String[] args) {

		if (args.length < 7) {
			System.out.println("too few arguments");
			System.out
					.println("datagenerator <host> <user> <pass> <db> <collection> <sensorsCount> <packagesPerSensor>");
			System.exit(1);
		}

		String host = args[0];
		String user = args[1];
		String pass = args[2];
		String dbString = args[3];
		String collection = args[4];
		int sensorsCount = Integer.parseInt(args[5]);
		int packagesPerSensor = Integer.parseInt(args[6]);

		// System.out.printf("Host: %s, user: %s, pass: %s\n", host, user, pass);

		DB db = getDb(host, user, pass, dbString);

		DBCollection coll = db.getCollection(collection);

		RandomSensorDataPopulator popper = new RandomSensorDataPopulator(coll);
		popper.populate(sensorsCount, packagesPerSensor);

	}

	private static DB getDb(String host, String user, String pass, String dbString) {

		try {
			MongoCredential cred = MongoCredential.createMongoCRCredential(user, dbString,
					pass.toCharArray());
			MongoClient client = new MongoClient(new ServerAddress(host), Arrays.asList(cred));
			return client.getDB(dbString);

		} catch (UnknownHostException e) {
			System.out.println("Error connectiong to Database: " + e);
			System.exit(1);
		}

		return null;
	}

}
